package main.test.sparklingGraphAPI

import ml.sparkling.graph.api.loaders.GraphLoading.LoadGraph
import ml.sparkling.graph.loaders.csv.GraphFromCsv.CSV
import ml.sparkling.graph.loaders.csv.GraphFromCsv.LoaderParameters.{Delimiter, Indexing, Quotation}
import ml.sparkling.graph.loaders.graphml.GraphFromGraphML.{GraphML, GraphProperties}
import org.apache.spark.graphx.{EdgeRDD, Graph, VertexId}
import org.apache.spark.sql.SparkSession
import org.apache.spark.{SparkConf, SparkContext}
class  LoadingGraph{}
object LoadingGraph {

  val SparkConf = new SparkConf().setMaster("local[*]").setAppName("LoadGraph")
  implicit val ctx:SparkContext=new SparkContext(SparkConf)
  def main(args: Array[String]): Unit = {


    /**
     * Main graph loading object is a LoadGraph
     * Parameters for configuration are set using using(parameter: Parameter) method.
     * Parameters are specific for each GraphLoader.
     *
     *
     *
     */
    //Loading from CSV
    /**
     * To load graph from csv file , you must use csv implementation of GraphLoad trait.
     */


    // initialize your SparkContext as implicit value so it will be passed automatically to graph loading API

    val filePath="data/your_graph_path.csv"

    val graph: Graph[Int, Int] =LoadGraph.from(CSV(filePath)).load()
    val edges: EdgeRDD[Int] = graph.edges
    println(edges.first().attr)
    println(edges.first().srcId)
    println(edges.first().dstId)

    // In order to change file format you can use parameters like:
    val filePath2="data/your_graph_path2.csv"
    val graph2: Graph[Int, Int] =LoadGraph.from(CSV(filePath2)).using(Delimiter(";")).using(Quotation("'")).load()
    val edges2: EdgeRDD[Int] = graph2.edges
    println(edges2.first().attr)
    println(edges2.first().srcId)
    println(edges2.first().dstId)

    // Loading graphs with vertex identifiers that are not numerical
    /**
     * Because in some cases vertices identifiers can be not numerical (username as string). You can load this kind of graph
     * specifying that Indexing is required:
     */
    val filePath3="data/your_graph_path3.csv"
    val graph3=LoadGraph.from(CSV(filePath3)).using(Indexing).load()
    graph3.edges.foreach(println(_))

    // Loading from GraphML
    /**
     * To load graph from GraphML XML file you must use GraphML
     * implementation of GraphLoader trait: http://sparkling-graph.github.io/sparkling-graph/latest/api/#ml.sparkling.graph.loaders.graphml.GraphFromGraphML$$GraphML$
     * That is simplest way of loading
     * standard GraphML XML file
     * (vertices are automatically indexed, and receive VertexId identifier ):
     */


    val filePath4="data/your_graph_path.xml"

    val graph4: Graph[GraphProperties, GraphProperties] =LoadGraph.from(GraphML(filePath4)).load()
//    val verticesIdsFromFile: Array[String] = graph4.vertices.map(_._2("id").asInstanceOf[String]).collect()
    val verticesIdsFromFile: Array[Any] = graph4.vertices.map(x=>x._2("name")).collect()

    val vtF: Array[GraphProperties] = graph4.vertices.map(x=>x._2).collect()
    val eIdsFromFile: Array[GraphProperties] = graph4.edges.map(x=>x.attr).collect()
    verticesIdsFromFile.foreach(println)

    // All attributes associated with vertices will be
    // puted into GraphProperties type which expands to
    // Map[String,Any]. By default each edge and vertex has id attribute.


    // Map(id -> e2)
    eIdsFromFile.foreach(println)

    // Map(name -> name0, type -> type0, id -> n0)
    vtF.foreach(println)







  }

}
